package com.zrrd.yunchmall.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.user.entity.Resource;
import com.zrrd.yunchmall.user.entity.Role;
import com.zrrd.yunchmall.user.service.IRoleService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/role")
@Api(tags = "权限服务-角色模块")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @ApiOperation("查询所有角色信息")
    @GetMapping("/listAll")
    public ResponseResult listAll() {
        // select id, name .... from ums_role
        return new ResponseResult(200, "查询成功", roleService.list());
    }

    @PostMapping("/create")
    public ResponseResult create(@RequestBody Role role){
        roleService.save(role);
        return new ResponseResult(200,"保存成功");
    }

    @PostMapping("/delete")
    public ResponseResult delete(int ids){
        roleService.removeById(ids);
        return new ResponseResult(200,"删除成功");
    }

    @GetMapping("/list")
    public ResponseResult list(int pageNum,int pageSize,String keyword){
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(keyword)){
            queryWrapper.eq("name",keyword);
        }
        IPage<Role> pageInfo = roleService.page(new Page<>(pageNum,pageSize),queryWrapper);
        return new ResponseResult(200,"查询成功",pageInfo);
    }


    @ApiOperation("查询角色能够访问的资源")
    @GetMapping("/listResource/{id}")
    public ResponseResult listResource(@ApiParam(required = true) @PathVariable int id){
        return new ResponseResult(200,"查询成功",roleService.listResource(id));
    }

    @ApiOperation("查询角色能够访问的菜单")
    @GetMapping("/listMenu/{id}")
    public ResponseResult listMenu(@ApiParam(required = true) @PathVariable int id){
        return new ResponseResult(200,"查询成功",roleService.listMenu(id));
    }

    @PostMapping("/allocMenu")
    public ResponseResult allocMenu(long roleId,String menuIds){
        //删除原有的
        //分配本次的
        roleService.allocMenu(roleId,menuIds);
        return new ResponseResult(200,"更新成功");
    }

    @PostMapping("/allocResource")
    public ResponseResult allocResource(long roleId,String resourceIds){
        roleService.allocResource(roleId,resourceIds);
        return new ResponseResult(200,"更新成功");
    }


    @PostMapping("/update/{id}")
    public ResponseResult update(@PathVariable int id,@RequestBody Role role){
        roleService.updateById(role);
        return new ResponseResult(200,"编辑成功");
    }

}
